<!DOCTYPE HTML>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../css/api.css" />
		<link rel="stylesheet" type="text/css" href="../../css/style.css" />
		<link rel="stylesheet" type="text/css" href="../../css/btnplus.css" />
		<style type="text/css">
			html,body { height:100%; background-color: #fff; }
			.share{ padding: 40px 15px 20px;}
			.share .con{ width: 280px; height: 420px; margin: 0 auto; padding: 0; text-align: center; color: #fff; border-radius: 4px; background: url(../../image/img_02.jpg); background-size: 100% 100%; box-shadow: 0 0 15px rgba(11,102,248,0.3);}
			.share div { padding:35px 0 15px; }
			.share div2{  margin: 0 auto; padding: 0; text-align: center; color: #fff; border-radius: 4px; background: url(../../image/img_02.jpg) no-repeat; background-size: 100% 100%; box-shadow: 0 0 15px rgba(0,0,0,0.3);}
			.share b{ display:block; font-size: 15px; font-weight: 500; line-height: 35px; padding-top: 10px;}
			.share p{ font-size: 12px; line-height: 26px;}
			.share .img{ margin: 0 auto; width: 70px; height: 70px; border: 0px #fff solid; border-radius: 50%;}
			.share .code{ margin: 30px auto; width: 150px; height: 150px;}
			.share button{ display:block; margin-top:35px; border: 0; width: 100%; height: 50px; color: #fff; font-size: 15px; background-color: #3262F1;}
		</style>
	</head>
	<body>
		<div class="share">
			<div class="con">
				<div>
					<img data-url="" src="../../image/defaultimg.png" id="avatar" class="img" />
					<b id="nickname">-</b>
					<p>我邀请你加入运运满，he我一起发货源抢运单</p>
					<img id="qrcode" src="../../image/kong.png" alt="" class="code" />
				</div>
			</div>
			<button tapmode="btnpress" onclick="openShare();">分享好友</button>
		</div>
	</body>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/app.js"></script>
	<script type="text/javascript" src="../../script/permission.js"></script>
	<script type="text/javascript">
		var myuid;
		apiready = function() {
			api.parseTapmode();
			api.getPrefs({
				key: 'user_info'
			}, function(ret, err) {
				if (ret && ret.value)
				{
					var userinfo = JSON.parse(ret.value);
					myuid = userinfo.uid;
					var avatar = userinfo.avatar;
					$api.text($api.byId('nickname'),userinfo.realname);
					$api.attr($api.byId('avatar'),'data-url',avatar);
					fnLoadImage($api.byId('avatar'));
					
					var pername = 'storage';
					var perdesc = '本机存储';
					if(!confirmPerextra(pername,perdesc,createQrcode)){
						return;
					}
					
					createQrcode();
				}
			});
			api.addEventListener({
                name: 'navitembtn'
            }, function(ret, err) {
                if (ret.type == 'left') {
					api.closeWin({
					});
                }
            });
		}
		function confirmPerextra(perm, permdesc, callback){
			var has = hasPermission(perm);
			if(!has || !has[0] || !has[0].granted){
				api.confirm({
					title: '提醒',
					msg: '该功能需要开启 “' + permdesc + '” 权限\n是否前往设置？',
					buttons: ['去设置', '取消']
				}, function(ret, err) {
					if(1 == ret.buttonIndex){
						reqPermission(perm, callback);
					}
					else
					{
						api.closeWin({
						});
						
					}
				});
				return false;
			}
			return true;
		}
		function createQrcode() {
			api.getPrefs({
				key: 'user_info'
			}, function(ret, err) {
				if (ret && ret.value)
				{
					var userinfo = JSON.parse(ret.value);
					var FNScanner = api.require('FNScanner');
					FNScanner.encodeImg({
						content: WebUrl + '/web/reg/?fromuid='+userinfo.mobile,
						saveImg: {
							path: 'fs://qrcode/qrcode'+userinfo.uid+'.png',
							w: 200,
							h: 200
						}
					}, function(ret, err) {
						if (ret.status) {
							$api.byId('qrcode').src = ret.imgPath;
							//alert(JSON.stringify(ret));
						} else {
							//alert(JSON.stringify(err));
						}
					});
				}
			});
			
		}
		function share(typeid) {
			//分享
			if(typeid < 2){
				//分享微信好友
				var wx = api.require('wx');
				wx.isInstalled(function(ret, err) {
					if (ret.installed) {
						
						var scene = 'session';
						if(typeid == 1) {
							scene = 'timeline';
						}
						
						wx.shareImage({
							apiKey: '',
							scene: scene,
							contentUrl: 'fs://qrcode/qrcode'+myuid+'.jpg'
						}, function(ret, err) {
							if (ret.status) {
								toast('分享成功');
							} else {
								toast("分享失败！");
							}
						});
					}
					else
					{
						toast("未安装微信客户端，无法分享");
					}
				});
				
			}
			else
			{
				var qq = api.require('QQPlus');
				qq.installed(function(ret, err) {
					if (ret.status) {
						var scene = 'QFriend';
						if(typeid == 3) {
							scene = 'QZone';
						}
						qq.shareImage({
							type : scene,
							imgPath: 'fs://qrcode/qrcode'+myuid+'.jpg'
						},function(ret,err){
							if (ret.status){
								toast("分享成功！");
							} else {
								toast("分享失败！");
							}
						});
					} else {
						toast("未安装QQ客户端，无法分享");
					}
				});
				
			}
		}
		function getImgandDown(typeid) {
			//获取分享海报并下载到本地
			api.showProgress({
				title: '请稍后...',
				text: '',
				modal: true
			});
			api.download({
				url: ApiUrl + '/v1/userqrcode/'+myuid,
				savePath: 'fs://qrcode/qrcode'+myuid+'.jpg',
				cache: false,
				allowResume: true
			}, function(ret, err) {
				if (ret.state == 1) {
					//下载成功
					api.hideProgress();
					share(typeid);
				}
			});
		}
		function openShare(){
			var dialogBox = api.require('dialogBox');
			dialogBox.actionMenu ({
				rect:{
					h: 170
				},
				texts:{
					 cancel: '取消'
				},
				items:[
				{
					text: '微信好友',
					icon: 'widget://res/sharebtns/wx.png'
				},{
					text: '朋友圈',
					icon: 'widget://res/sharebtns/pyq.png'
				},{
					text: 'QQ好友',
					icon: 'widget://res/sharebtns/qq.png'
				},{
					text: 'QQ空间',
					icon: 'widget://res/sharebtns/qqkj.png'
				}
				],
				styles: {
					bg: '#FFF',
					corner: 6,
					column: 4,
					itemText: {
						color: '#333',
						size: 13,
						marginT: 10
					},
					itemIcon: {
						size: 50
					},
					cancel: {
						bg: '#fff',
						color: '#333',
						h: 55,
						size: 14
					}
				},
				tapClose: true
			}, function(ret){
				if(ret.eventType == 'click')
				{
					dialogBox.close({
						dialogName: 'actionMenu'
					});
					var index = ret.index;
					getImgandDown(index);
				}
				else if(ret.eventType == 'cancel')
				{
					dialogBox.close({
						dialogName: 'actionMenu'
					});
				}
				
			});
		}
		function fnLoadImage(obj) {
			var dataUrl = $api.attr(obj, 'data-url');
			if(dataUrl!=''){
				api.imageCache({
					url: dataUrl
				}, function(ret, err) {
					if(ret && ret.status == true){
						obj.src = ret.url;
					}
				});
			}
		}
	</script>
</html>